﻿using UnityEngine;
using System.Collections;

public class GameScript : MonoBehaviour 
{
    public GameObject persistentManager;
    public GameObject score;
    public GameObject timeText;
    public GameObject addedScorePrefab;
    public GameObject[] faces;   
    public float hitScore;
    public float missScore;
    private Transform middle;
    private GameObject currentFace; 
    private FaceMovementScript currentFaceMovementScript;
    private float timeLeft = 10;
    private Color currentColor;
    //private int maxAmmo = 6;
    //private int currentAmmo = 0;

    private int intensity = 1;

    private bool validFaceOnField = false;

	void Start () 
    {
        middle = transform.FindChild("Middle").transform;
        CreateNewFace();
	}

    public void Reset()
    {        
        currentFace.GetComponent<FaceMovementScript>().FaceCompleted();
        intensity = 1;
        timeLeft = 10;
        SetTimeColor();
        UpdateScoreText();
        UpdateTimeLeft();
        CreateNewFace();
    }

    public void GameStartInitialization()
    {
        //currentAmmo = maxAmmo;
    }

    //public bool CanShoot()
    //{
    //    return currentAmmo > 0;
    //}

    //public void Shoot()
    //{
    //    if (currentAmmo > 0)
    //    {
    //        currentAmmo--;
    //        if (currentAmmo == 0)
    //        {
    //            StartCoroutine("ReloadGun");
    //        }
    //    }
    //}

    //private IEnumerator ReloadGun()
    //{
    //    yield return new WaitForSeconds(2); 
    //    currentAmmo = maxAmmo;
    //}
	
	void Update () 
    {
        if (!validFaceOnField)
        {
            CreateNewFace();
        }

        UpdateTimeLeft();

        if (currentFace.transform.FindChild("Teeth").GetComponent<TeethManagerScript>().NoMoreBadTeeth())
        {
            TeethParticleScript.instance.StopAndClear();            
            AddNextFace();
        }
	}

    void AddNextFace()
    {
        validFaceOnField = currentFaceMovementScript.FaceCompleted();
        float addedTime = 3;
        timeLeft += addedTime;
        persistentManager.GetComponent<PersistentManagerScript>().InstantiateAddedTime(addedTime);
    }

    void CreateNewFace()
    {
        validFaceOnField = true;
        int faceIndex = Random.Range(0, faces.Length);
        currentFace = Instantiate(faces[faceIndex], middle.position + new Vector3(12, 0), new Quaternion()) as GameObject;
        currentFace.transform.FindChild("Teeth").GetComponent<TeethManagerScript>().OnSpawn();
        currentFace.name = faces[Random.Range(0, faces.Length)].name;
        currentFace.transform.parent = transform;
        currentFaceMovementScript = currentFace.GetComponent<FaceMovementScript>();
        currentFaceMovementScript.intensity = intensity;
        LastFace.instance.SetLastFace(faceIndex);
        intensity++;  
    }

    void AddScore(float score)
    {
        persistentManager.GetComponent<PersistentManagerScript>().AddToScore(score);
        UpdateScoreText();
    }

    void UpdateScoreText()
    {
       score.GetComponent<GUIText>().text = "Score: " + persistentManager.GetComponent<PersistentManagerScript>().GetCurrentScore();
    }

    void UpdateTimeLeft()
    {
        SetTimeColor(); 
        if (currentFaceMovementScript.IsEglibleForOperation())
        {
            timeLeft -= Time.deltaTime;                       

            if (timeLeft <= 0)
            {
                timeLeft = 0;
                BackGroundParticleScript.instance.Stop();           
                if(!persistentManager.GetComponent<PersistentManagerScript>().IsFading())
                    persistentManager.GetComponent<PersistentManagerScript>().Fade(FadeDir.outF);             
            }

        }
        timeText.GetComponent<GUIText>().text = "Time Left: " + Mathf.Round(timeLeft);        
    }

    void SetTimeColor()
    {
        Color color = Color.white;

        if (timeLeft < 9 && timeLeft >= 6)     
            color = Color.yellow;       

        else if (timeLeft < 6 && timeLeft >= 3)
            color = new Color(1.0f, 0.5f, 0);

        else if (timeLeft < 3)
            color = Color.red;

      timeText.GetComponent<GUIText>().color = color;

      if (   color != Color.white 
          && currentColor != color
          && currentFace.GetComponent<FaceMovementScript>().IsEglibleForOperation())
      {
          BackGroundParticleScript.instance.StartAt(transform.position);     
          BackGroundParticleScript.instance.SetColor(color);
      }

      currentColor = color;
    }

}
